import { Approval, ApprovalStatus, ApprovalType, FormFieldValue } from '../types/index';

// 模拟当前用户
export const currentUser = {
  id: 'user001',
  name: '张三'
};

// 模拟审批数据
export const mockApprovals: Approval[] = [
  {
    id: 'approval001',
    type: 1,
    typeName: '请假申请',
    creator: {
      id: 'user002',
      name: '李四'
    },
    createTime: '2024-03-20 10:00:00',
    status: ApprovalStatus.PENDING,
    currentApprover: {
      id: 'user001',
      name: '张三'
    },
    fields: [
      {
        key: 'title',
        label: '标题',
        value: '请假申请-婚礼',
        type: 'text'
      },
      {
        key: 'leaveType',
        label: '请假类型',
        value: '3',
        type: 'select',
        options: [
          { value: '1', label: '年假' },
          { value: '2', label: '病假' },
          { value: '3', label: '事假' }
        ]
      },
      {
        key: 'startDate',
        label: '开始日期',
        value: '2024-03-25',
        type: 'date'
      },
      {
        key: 'endDate',
        label: '结束日期',
        value: '2024-03-27',
        type: 'date'
      },
      {
        key: 'content',
        label: '请假原因',
        value: '申请请假3天，参加朋友婚礼',
        type: 'textarea'
      }
    ],
    approvalHistory: []
  },
  {
    id: 'approval002',
    type: 2,
    typeName: '报销申请',
    creator: {
      id: 'user003',
      name: '王五'
    },
    createTime: '2024-03-19 14:30:00',
    status: ApprovalStatus.APPROVED,
    currentApprover: {
      id: 'user001',
      name: '张三'
    },
    approvalHistory: [
      {
        operatorId: 'user001',
        operatorName: '张三',
        operation: ApprovalStatus.APPROVED,
        comment: '同意报销',
        operateTime: '2024-03-19 15:00:00'
      }
    ],
    fields: [
      {
        key: 'title',
        label: '标题',
        value: '报销申请-差旅费',
        type: 'text'
      },
      {
        key: 'amount',
        label: '报销金额',
        value: '2000',
        type: 'number'
      },
      {
        key: 'expenseType',
        label: '报销类型',
        value: '1',
        type: 'select',
        options: [
          { value: '1', label: '差旅费' },
          { value: '2', label: '办公用品' },
          { value: '3', label: '其他' }
        ]
      },
      {
        key: 'content',
        label: '报销说明',
        value: '3月18日-19日出差深圳产生的交通和住宿费用',
        type: 'textarea'
      }
    ]
  },
  {
    id: 'approval003',
    type: 4,
    typeName: '采购申请',
    creator: {
      id: 'user003',
      name: '王五'
    },
    createTime: '2024-03-21 09:30:00',
    status: ApprovalStatus.PENDING,
    currentApprover: {
      id: 'user001',
      name: '张三'
    },
    approvalHistory: [],
    fields: [
      {
        key: 'title',
        label: '标题',
        value: '采购申请-办公设备',
        type: 'text'
      },
      {
        key: 'category',
        label: '采购类别',
        value: '2',
        type: 'select',
        options: [
          { value: '1', label: '办公用品' },
          { value: '2', label: '电子设备' },
          { value: '3', label: '软件服务' },
          { value: '4', label: '其他' }
        ]
      },
      {
        key: 'itemList',
        label: '采购清单',
        value: '1. 显示器 * 2 (预算 2000元/个)\n2. 键盘 * 2 (预算 300元/个)',
        type: 'textarea'
      },
      {
        key: 'amount',
        label: '采购金额',
        value: '4600',
        type: 'number'
      },
      {
        key: 'supplier',
        label: '建议供应商',
        value: '京东商城',
        type: 'text'
      },
      {
        key: 'expectedDate',
        label: '期望到货日期',
        value: '2024-04-01',
        type: 'date'
      },
      {
        key: 'content',
        label: '采购说明',
        value: '部门新入职员工办公设备采购',
        type: 'textarea'
      }
    ]
  },
  {
    id: 'approval004',
    type: 3,
    typeName: '出差申请',
    creator: {
      id: 'user002',
      name: '李四'
    },
    createTime: '2024-03-22 11:00:00',
    status: ApprovalStatus.PENDING,
    currentApprover: {
      id: 'user001',
      name: '张三'
    },
    approvalHistory: [],
    fields: [
      {
        key: 'title',
        label: '标题',
        value: '出差申请-客户拜访',
        type: 'text'
      },
      {
        key: 'destination',
        label: '目的地',
        value: '广州',
        type: 'text'
      },
      {
        key: 'startDate',
        label: '开始日期',
        value: '2024-03-25',
        type: 'date'
      },
      {
        key: 'endDate',
        label: '结束日期',
        value: '2024-03-26',
        type: 'date'
      },
      {
        key: 'transportation',
        label: '交通方式',
        value: '2',
        type: 'select',
        options: [
          { value: '1', label: '飞机' },
          { value: '2', label: '高铁' },
          { value: '3', label: '火车' },
          { value: '4', label: '汽车' }
        ]
      },
      {
        key: 'budget',
        label: '预算金额',
        value: '1500',
        type: 'number'
      },
      {
        key: 'content',
        label: '出差事由',
        value: '前往广州拜访重点客户，商讨合作事宜',
        type: 'textarea'
      }
    ]
  }
];

// 审批类型定义
export const approvalTypes: ApprovalType[] = [
  {
    id: 1,
    name: '请假申请',
    icon: '🏖️',
    fields: [
      {
        key: 'title',
        label: '标题',
        type: 'text',
        placeholder: '请输入标题',
        required: true
      },
      {
        key: 'leaveType',
        label: '请假类型',
        type: 'select',
        options: [
          { value: '1', label: '年假' },
          { value: '2', label: '病假' },
          { value: '3', label: '事假' }
        ],
        required: true
      },
      {
        key: 'startDate',
        label: '开始日期',
        type: 'date',
        required: true
      },
      {
        key: 'endDate',
        label: '结束日期',
        type: 'date',
        required: true
      },
      {
        key: 'content',
        label: '请假原因',
        type: 'textarea',
        placeholder: '请详细描述请假原因',
        required: true
      }
    ]
  },
  {
    id: 2,
    name: '报销申请',
    icon: '💰',
    fields: [
      {
        key: 'title',
        label: '标题',
        type: 'text',
        placeholder: '请输入标题',
        required: true
      },
      {
        key: 'amount',
        label: '报销金额',
        type: 'number',
        placeholder: '请输入报销金额',
        required: true
      },
      {
        key: 'expenseType',
        label: '报销类型',
        type: 'select',
        options: [
          { value: '1', label: '差旅费' },
          { value: '2', label: '办公用品' },
          { value: '3', label: '其他' }
        ],
        required: true
      },
      {
        key: 'content',
        label: '报销说明',
        type: 'textarea',
        placeholder: '请详细描述报销内容',
        required: true
      }
    ]
  },
  {
    id: 3,
    name: '出差申请',
    icon: '✈️',
    fields: [
      {
        key: 'title',
        label: '标题',
        type: 'text',
        placeholder: '请输入标题',
        required: true
      },
      {
        key: 'destination',
        label: '目的地',
        type: 'text',
        placeholder: '请输入出差目的地',
        required: true
      },
      {
        key: 'startDate',
        label: '开始日期',
        type: 'date',
        required: true
      },
      {
        key: 'endDate',
        label: '结束日期',
        type: 'date',
        required: true
      },
      {
        key: 'transportation',
        label: '交通方式',
        type: 'select',
        options: [
          { value: '1', label: '飞机' },
          { value: '2', label: '高铁' },
          { value: '3', label: '火车' },
          { value: '4', label: '汽车' }
        ],
        required: true
      },
      {
        key: 'budget',
        label: '预算金额',
        type: 'number',
        placeholder: '请输入预算金额',
        required: true
      },
      {
        key: 'content',
        label: '出差事由',
        type: 'textarea',
        placeholder: '请详细描述出差目的和工作内容',
        required: true
      }
    ]
  },
  {
    id: 4,
    name: '采购申请',
    icon: '🛒',
    fields: [
      {
        key: 'title',
        label: '标题',
        type: 'text',
        placeholder: '请输入标题',
        required: true
      },
      {
        key: 'category',
        label: '采购类别',
        type: 'select',
        options: [
          { value: '1', label: '办公用品' },
          { value: '2', label: '电子设备' },
          { value: '3', label: '软件服务' },
          { value: '4', label: '其他' }
        ],
        required: true
      },
      {
        key: 'itemList',
        label: '采购清单',
        type: 'textarea',
        placeholder: '请列出需要采购的物品清单，包括名称、规格、数量等',
        required: true
      },
      {
        key: 'amount',
        label: '采购金额',
        type: 'number',
        placeholder: '请输入采购总金额',
        required: true
      },
      {
        key: 'supplier',
        label: '建议供应商',
        type: 'text',
        placeholder: '请输入建议的供应商（选填）',
        required: false
      },
      {
        key: 'expectedDate',
        label: '期望到货日期',
        type: 'date',
        required: true
      },
      {
        key: 'content',
        label: '采购说明',
        type: 'textarea',
        placeholder: '请说明采购用途和必要性',
        required: true
      }
    ]
  }
];

// 获取审批列表
export function getApprovalList(): Promise<Approval[]> {
  return Promise.resolve(mockApprovals);
}

// 获取待办列表
export function getTodoList(): Promise<Approval[]> {
  return Promise.resolve(
    mockApprovals.filter(
      item => 
        item.status === ApprovalStatus.PENDING && 
        item.currentApprover.id === currentUser.id
    )
  );
}

// 获取审批详情
export function getApprovalDetail(id: string): Promise<Approval | undefined> {
  return Promise.resolve(mockApprovals.find(item => item.id === id));
}

// 处理审批
export function handleApproval(
  approvalId: string, 
  operation: ApprovalStatus, 
  comment: string
): Promise<boolean> {
  const approval = mockApprovals.find(item => item.id === approvalId);
  if (approval) {
    approval.status = operation;
    approval.approvalHistory.push({
      operatorId: currentUser.id,
      operatorName: currentUser.name,
      operation,
      comment,
      operateTime: new Date().toLocaleString()
    });

    // 如果是转交操作，更新当前审批人
    if (operation === ApprovalStatus.TRANSFERRED) {
      approval.currentApprover = {
        id: 'user003',
        name: '王五'
      };
    }

    return Promise.resolve(true);
  }
  return Promise.resolve(false);
}

// 添加新的审批
export function createApproval(type: number, fields: FormFieldValue[]): Promise<boolean> {
  const approvalType = approvalTypes.find(t => t.id === type);
  if (!approvalType) return Promise.resolve(false);

  const approval: Approval = {
    id: `approval${mockApprovals.length + 1}`,
    type,
    typeName: approvalType.name,
    creator: {
      id: currentUser.id,
      name: currentUser.name
    },
    createTime: new Date().toLocaleString(),
    status: ApprovalStatus.PENDING,
    currentApprover: {
      id: 'user002',
      name: '李四'
    },
    fields,
    approvalHistory: []
  };

  mockApprovals.unshift(approval);
  return Promise.resolve(true);
}

// 获取审批类型定义
export function getApprovalType(typeId: number): Promise<ApprovalType | undefined> {
  return Promise.resolve(approvalTypes.find(type => type.id === typeId));
} 